#include <iostream>
using namespace std;

struct ListNode {
    int val;
    ListNode *next;
    ListNode() : val(0), next(nullptr) {}
    ListNode(int x) : val(x), next(nullptr) {}
    ListNode(int x, ListNode *next) : val(x), next(next) {}
};

class Solution {
public:
    ListNode* swapPairs(ListNode* head) {
        ListNode* myhead = new ListNode(0);
        myhead->next = head;
        ListNode* pre = myhead;
        ListNode* cur = head;
        while(cur->next && cur){
            ListNode* temp = cur->next;
            cur->next = temp->next;
            temp->next = cur;
            pre->next = temp;
            pre = cur;
            cur = cur->next;
        }
        return myhead;
    }
};